// pages/my/coupon.js
import api from '../../api/index'
import { userInfoStore } from '../../store/userInfo'
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:1,
    status:1,
    dataList: [],
    uploading: false,
    page:1,
    current:1,
    total:0,
    pages:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self = this;
    this.setData({ dataList: [], uploading: false}, () => {
      self.getList();
    })
  },

  onShow: function(){
    
  },

  selectItem:function(e){
    let self=this;
    let t = e.currentTarget.dataset.type;
    this.setData({ page: 1, dataList: [], uploading: false, type: t},()=>{
      self.getList();
    });
  },

  selectStatus:function(e){
    let self = this;
    let s = e.currentTarget.dataset.status;
    this.setData({ page: 1, dataList: [], uploading: false, status: s }, () => {
      self.getList();
    });
  },

  use:function(e){
    let coupon = e.currentTarget.dataset.item;
    let param = {
      openId: userInfoStore.userInfo.openId,
      userCouponId: coupon.id
    }
    let self = this;
    api.confirmCoupon(param).then(res => {
      self.setData({ page: 1, dataList: [], uploading: false}, () => {
        self.getList();
      });
      wx.showModal({
        title: '提示',
        content: '已确认成功',
        showCancel:false
      })
    }, err => {
      wx.showToast({
        icon: "none",
        title: err
      })
    });
  },

  completeRefund:function(e){
    let self = this;
    let id = e.currentTarget.dataset.id;
    let param = {
      openId: userInfoStore.userInfo.openId,
      userCouponId: id
    }
    api.completeCouponRefund(param).then(res => {
      self.setData({ page: 1, dataList: [], uploading: false, status:  5}, () => {
        self.getList();
      });
    }, err => {
      wx.showToast({
        icon: "none",
        title: err
      })
    });
  },

  getList: function (){
    let self=this;
    //openId: userInfoStore.userInfo.openId,
    //openId: 'ofhiR4o14HgFB0zd2RYJ-pjC0trQ',
    let param = {
      openId: userInfoStore.userInfo.openId,
      type: this.data.type,
      status: this.data.status,
      page: this.data.page,
      pageSize: 20
    }

    api.getshopreceivecouponlist(param).then(res => {
      console.log(res);
      let { result } = res;
      if (self.data.uploading) {
        if (self.data.page == self.data.pages){
          let number = self.data.total - self.data.current*20;
          let len = result.records.length;
          for (var i = (len - number); i <len;i++){
            self.data.dataList.push(result.records[i]);
          }
        } else {
          self.data.dataList.push(...result.records);
        }
        self.setData({
          dataList: self.data.dataList,
          current: result.current,
          total: result.total,
          pages: result.pages,
          uploading: false
        })
      } else {
        self.setData({
          dataList: result.records,
          current: result.current,
          total: result.total,
          pages: result.pages,
          uploading: false
        })
      }
      
      wx.stopPullDownRefresh();
    }, err => {
      console.log("err");
      self.setData({ uploading: false });
      wx.stopPullDownRefresh();
    });
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let self=this;
    this.setData({ page: 1, dataList: [], uploading:false},()=>{
      self.getList();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.uploading && this.data.current < this.data.pages) {
      let self=this;
      let next = this.data.current+1;
      this.setData({ page: next, uploading: true }, () => {
        self.getList();
      });
    }
  }
})